package com.zxy.ziems.server.foms.service;

import com.alibaba.fastjson.JSONObject;
import com.zxy.btp.redis.utils.RedisUtil;
import com.zxy.ziems.server.utils.BusinessExceptionUtil;
import com.zxy.ziems.server.utils.OkHttpUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @Author hanyongliang
 */
public abstract class BaseRpcService {

    @Autowired
    private RedisUtil redisUtil;

    public abstract String getBaseUrl();

    public String buildUrl(String uri) {
        StringBuilder sb = new StringBuilder(getBaseUrl());
        sb.append(uri);
        return sb.toString();
    }

    public String requestData(String url, Map<String, Object> params) {
        Map<String, Object> resposnseMap = null;
        try {
            String resposnse = OkHttpUtil.postJson(url, JSONObject.toJSONString(params));
            resposnseMap = JSONObject.parseObject(resposnse, Map.class);
        } catch (Exception e) {
            throw BusinessExceptionUtil.exception(e.getMessage());
        }
        return JSONObject.toJSONString(resposnseMap.get("data"));
    }

    public void setRedisCache(String id, String key, Object value, Long expire) {
        redisUtil.set(id.concat(":").concat(key), value, expire);
    }

    public Object getRedisCache(String id, String key) {
        Object value = redisUtil.get(id.concat(":").concat(key));
        return value;
    }


}
